package com.app.config.swagger;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.MappedInterceptor;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.List;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;

/**
 * Swagger 配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Value("${swagger.basic.username}")
    private String username;
    @Value("${swagger.basic.password}")
    private String password;

    /* 必须在此处配置拦截器,要不然拦不到swagger的静态资源 */
    @Bean
    @ConditionalOnProperty(name = "swagger.basic.enable", havingValue = "true")
    public MappedInterceptor getMappedInterceptor() {
        return new MappedInterceptor(new String[]{"/swagger-ui.html", "/webjars/**"},
                new SwaggerInterceptor(username, password));
    }


    @Bean
    public Docket docket() {
        return buildDocket("Default",
                "com.app.modules",
                "/**")
                .enable(true);
    }

    private Docket buildDocket(String groupName, String basePackage, String antPattern) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.ant(antPattern))
                .build()
                .apiInfo(apiInfo())
                .directModelSubstitute(Temporal.class, String.class);
               // .globalOperationParameters(getParameterList());

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("多数据源统接口文档")
                .description("多数据源统接口API文档")
                .version("1.0.0")
                .contact(new Contact("多数据源统接口文档", "", ""))
                .license("")
                .licenseUrl("")
                .build();
    }

//    private List<Parameter> getParameterList() {
//        ParameterBuilder clientIdTicket = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<Parameter>();
//        clientIdTicket.name("token").description("token令牌")
//                .modelRef(new ModelRef("string"))
//                .parameterType("header")
//                .required(false).build(); //设置false，表示clientId参数 非必填,可传可不传！
//        pars.add(clientIdTicket.build());
//        return pars;
//    }




}
